package cn.jishiyu11.xeqb.adapter

import android.content.Context
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import cn.jishiyu11.xeqb.EntityClass.FragmengData.MsgData
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.webview.X5WebViewActivity

/**
 * Created by jsy_zj on 2018/1/15.
 */
class MSGadapter(context: Context, msgList: MutableList<MsgData.Data>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var context: Context? = null
    var list: MutableList<MsgData.Data>? = null
    var inflater: LayoutInflater? = null
    var TYPE_FOOT = 0
    var TYPE_NORMAL = 1

    init {
        this.context = context
        this.list = msgList
        this.inflater = LayoutInflater.from(this.context)

    }


    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        var view:View? = null
            when(viewType){
                TYPE_FOOT ->{
                    view = inflater?.inflate(R.layout.footermore,parent,false)
                    return ViewHolderLoadMore(view,context!!)
                }
                TYPE_NORMAL->{
                    view = inflater?.inflate(R.layout.msg_recycler_item_text1,parent,false)
                    return ViewHolder(view,context!!)
                }
            }
        return ViewHolder(view,context!!)
    }

    override fun getItemCount(): Int {
        return if (list!=null && list?.size!! >1) list?.size!! + 1 else 0
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int)  {
        if (position >=list?.size!!){
        }else {
            when (holder) {
                is ViewHolder -> {
                    var msg = list?.get(position)
                    holder?.title!!.text = msg?.title
                    if (!msg?.intro.isNullOrEmpty()) {
                        holder?.content!!.text = msg?.intro
                    } else {
                        holder?.content.visibility = View.GONE
                    }
                    holder?.time!!.text = msg?.addtime
                    holder?.layout!!.setOnClickListener {
                        var intent = Intent(this.context, X5WebViewActivity::class.java)
                        intent.putExtra("url", msg?.links)
                        this.context?.startActivity(intent)
                    }
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position>=list?.size!!)TYPE_FOOT else TYPE_NORMAL
    }


    class ViewHolder(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var layout: LinearLayout = itemView?.findViewById(R.id.msg_recycler_item_text1_layout)!!
        var title: TextView = itemView?.findViewById(R.id.msg_recycler_item_text1_title)!!
        var content: TextView = itemView?.findViewById(R.id.msg_recycler_item_text1_content)!!
        var time: TextView = itemView?.findViewById(R.id.msg_recycler_item_text1_time)!!
    }
    class ViewHolderLoadMore(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var gridView: LinearLayout = itemView?.findViewById(R.id.footer_layout)!!
    }

}